package com.gljx.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gljx.constant.Constant;
import com.gljx.web.dao.CommonMapper;
import com.gljx.web.Entity.Company;
import com.gljx.web.Entity.Period;
import com.gljx.web.Entity.StatisticalParameter;
import com.gljx.web.service.CommonService;

@Service
public class CommonServiceImpl implements CommonService{

	@Autowired
	private CommonMapper commonDao;
	
	public Map<String, Object> selectCompanyPage(String rows,
												 String page,
												 String company		//公司
												 ){
		//获取当前用户ID
		Session session = SecurityUtils.getSubject().getSession();
		Integer userId = (Integer) session.getAttribute(Constant.UserId_session);
		//页码，页数
		Integer pageNum = Integer.valueOf(page);
		Integer pageSize = Integer.valueOf(rows);
		
		Map <String,Object > data = new HashMap<String, Object>();
		data.put("Pindex", pageNum);
		//依据当前用户选择公司次数多少的 顺序 查询公司表
		String sql=  "select FID,flongnumber,FNAME_L2,nvl(ct.times,0) companyTimes"
				+ " from  T_ORG_Company "
					+ "left join (select times,company_id "
								+ "from t_e_company_times "
								+ "where user_id="+userId+") ct "
					+ "on ct.company_id=fid ";
		//公司名称的 模糊查询
		if(company != null && !"".equals(company)){
			sql+= " where FNAME_L2 like '%"+company+"%'" ;
		}
		sql +=" order by companyTimes desc ";
		//套用分页存储过程
		data.put("Psql",sql);
		data.put("Psize", pageSize);
		commonDao.selectCompanyPage(data);
		
		Map <String,Object > dataMap = new HashMap<String, Object>();
		dataMap.put("total", data.get("Pcount"));
		dataMap.put("rows", data.get("v_cur"));
		return dataMap;
	}
	
	public Map<String, Object> selectDept(String rows,
										 String page,
										 String dept,		//部门
										 String companyLongFnumber//公司长编码
										 ){
		//页码，页数
		Integer pageNum = Integer.valueOf(page);
		Integer pageSize = Integer.valueOf(rows);
		
		Map <String,Object > data = new HashMap<String, Object>();
		data.put("Pindex", pageNum);
		//依据公司长编码，获取其下的部门
		String sql=  "select FID,FNAME_L2,flongnumber "
				+ "from t_pm_usergroup where flongnumber like '"+companyLongFnumber+"%'";
		//部门名称的 模糊查询
		if(dept != null && !"".equals(dept)){
		sql+= " where FNAME_L2 like '%"+dept+"%'" ;
		}
		sql +=" order by FNAME_L2 desc ";
		//套用分页存储过程
		data.put("Psql",sql);
		data.put("Psize", pageSize);
		commonDao.selectDept(data);
		
		return data;
	}
	
	public List<Company> selectCompany(){
		return commonDao.selectCompany();
	}
	
	@Override
	public List<Period> selectPeriod() {
		return commonDao.selectPeriod();
	}

	@Override
	public List<Map<String, Object>> selectCurrency() {
		return commonDao.selectCurrency();
	}

	@Override
	public List<StatisticalParameter> selectNameByStaticId(StatisticalParameter propertie) {
		
		return commonDao.selectNameByStaticId(propertie);
	}

	@Override
	public Company selectCompanyById(String FID) {
		return commonDao.selectCompanyById(FID);
	}

}
